package com.dev;
import android.graphics.Bitmap;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.app.Application;
public class HtmlBrowser extends LinearLayout{
	LinearLayout topbar;
	IconView icon;
	TextView title;
	ProgressBar progressBar;
	public WebView webView;
	public WebSettings webSettings;
	public HtmlBrowser(MainActivity ma){
		super(ma);
		setOrientation(VERTICAL);
		webView=new WebView(ma);
		webView.setWebViewClient(new WebViewClient_());
		webView.setWebChromeClient(new WebChromeClient_());
		webSettings=webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webSettings.setAllowFileAccess(true);
		webSettings.setAllowContentAccess(true);
		icon=new IconView(ma);
		title=new TextView(ma);
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
		title.setSingleLine();
		topbar=new LinearLayout(ma);
		topbar.setOrientation(HORIZONTAL);
		topbar.addView(icon,-1,-2);
		topbar.addView(title,new LayoutParams(0,-1,1));
		progressBar=new ProgressBar(ma,null,android.R.style.Widget_ProgressBar_Horizontal);
		progressBar.setIndeterminate(false);
		addView(topbar,-1,(int)(30*ma.dp));
		addView(progressBar,-1,(int)(4*ma.dp));
		addView(webView,new LayoutParams(-1,0,1));
	}
	class WebChromeClient_ extends WebChromeClient{
		@Override
		public void onReceivedIcon(WebView view,Bitmap icon){
			super.onReceivedIcon(view,icon);
			HtmlBrowser.this.icon.setImageBitmap(icon);
		}
		@Override
		public void onReceivedTitle(WebView view,String title){
			super.onReceivedTitle(view,title);
			HtmlBrowser.this.title.setText(title);
		}
		@Override
		public void onProgressChanged(WebView view,int newProgress){
			super.onProgressChanged(view,newProgress);
			progressBar.setProgress(newProgress);
		}
	}
	class WebViewClient_ extends WebViewClient{
		@Override
		public void onPageStarted(WebView view,String url,Bitmap favicon){
			super.onPageStarted(view,url,favicon);
			progressBar.setVisibility(View.VISIBLE);
		}
		@Override
		public boolean shouldOverrideUrlLoading(WebView view,WebResourceRequest request){
			//WebView.HitTestResult hit = view.getHitTestResult();
			return super.shouldOverrideUrlLoading(view,request);
			//return true;
		}
		@Override
		public boolean shouldOverrideUrlLoading(WebView view,String url){
			view.loadUrl(url);
			return super.shouldOverrideUrlLoading(view,url);
		}
		@Override
		public void onPageFinished(WebView view,String url){
			super.onPageFinished(view,url);
			progressBar.setVisibility(View.GONE);
		}
	}
}
